﻿/*
*   学员信息页面
*/
angular.module('jisuStuHome')
.controller('questionListController', ['$scope', '$rootScope', '$state', 'StuRequestTrain', 'interlocutionservice', 'sysUserInfo', 'Msg', 'T', 'LS', function ($scope, $rootScope, $state, ST, IS, sysUserInfo, Msg, T, LS) {


    //初始
    $scope.init = function () {
         $scope.userInfo = sysUserInfo.getuserinfo(); //用户
        $scope.colInfo = LS.getLS($scope.userInfo.user_ID + '_col'); //当前列表所属栏目对象
        $scope.backHome();
        $scope.isJoin=false;//是否已加入专栏
        //查询的参数
        $scope.params = {
            columnid:$scope.colInfo.columnId,
            orgid: sysUserInfo.getuserinfo().organization_ID,
            userid:sysUserInfo.getuserinfo().user_ID,
            powerLV:sysUserInfo.getuserinfo().powerLV,
            searchTxt: "",
            orderby: "1",
            state:"2",
            pageIndex: 1,
            pageSize: 10,
            type:"1",
        };
        if($scope.userInfo.powerLV==1||$scope.userInfo.powerLV==2||$scope.colInfo.userXid.indexOf($scope.userInfo.user_ID) != -1){
            $scope.params.state="0"
        }
    }
   
     //返回主页，专栏列表
    $scope.backHome = function () {
        $rootScope.parstr = "home.interlocution";
        //返回首页
        if ($scope.colInfo == null || $scope.colInfo == "" || $scope.colInfo == undefined) {
            $rootScope.twoparstr = "home.interlocution";
            $state.go($rootScope.twoparstr);
        } 
    }
    //返回首页
    $scope.goHome=function(){
        $rootScope.twoparstr = "home.interlocution";
        $state.go($rootScope.twoparstr);
    }
     $scope.init();
    //返回列表
    //路由跳转详情页面
    $scope.openQuestInfo = function (item) {
        $rootScope.twoparstr = "home.interlocution.questiondetail";
        $rootScope.parstr = "home.interlocution";
        LS.setLS(sysUserInfo.getuserinfo().user_ID + '_ques', item);
        $state.go($rootScope.twoparstr);
    }
    /**********************************查询***********************************/
    $scope.joinState=0;//加入专栏的状态  0什么也不是，初始，1申请中，2失败  3通过
    //是否加入专栏
    $scope.isJoinColumn = function () {
        //1.加密状态
        if($scope.colInfo.columnState==1){
                IS.IsJoinColumn({userid:sysUserInfo.getuserinfo().user_ID,colnumid:$scope.colInfo.columnId}).then(function (response) {
                    if (response.errorcode == "0") {
                        if(response.datas.length>0){
                            $scope.isJoin=true;
                            $scope.joinState=response.datas[0].state;
                            $scope.getList();
                        }
                    } else {
                        Msg.error("查询异常!")
                    }
                }, function errorCallback() {
                    $scope.Loading = false;
                    console.log("连接服务器失败！");
                });
        //公开
        }else{
            $scope.isJoin=true;
            $scope.getList();
        }
        
    }
    //查询列表
    $scope.getList = function (index) {
        if(!$scope.isJoin){
           // Msg.warning("您无权限查看该专栏！");
            return;
        }
        IS.findQuestionList($scope.params).then(function (response) {
            $scope.Loading = false;
            if (response.errorcode == "0") {
                $scope.quesList = response.datas;
                $scope.paginationConf.totalItems = response.numCount;
                $scope.paginationConf.currentPage = response.pageIndex;
                $scope.quesList.forEach(function(data,index){
                    if(data.isTop==undefined){
                        data.isTop=false;
                    }
                });
            } else {
                Msg.error("查询异常!")
            }
        }, function errorCallback() {
            $scope.Loading = false;
            console.log("连接服务器失败！");
        });
    }
    //申请加入
    $scope.JoinColumn = function () {
        if($scope.joinState==1){
            Msg.warning("已提交申请，请耐心等候审核！");
            return;
        }
        IS.joinSpecialColumn({orgid:sysUserInfo.getuserinfo().organization_ID,userid:sysUserInfo.getuserinfo().user_ID,username:sysUserInfo.getuserinfo().user_Name,userimg:sysUserInfo.getuserinfo().user_Img,colnumid:$scope.colInfo.columnId,colnumname:$scope.colInfo.columnName}).then(function (response) {
            if(response.errorcode==0){
                $scope.isJoin=!$scope.isJoin;
                if($scope.isJoin){
                    Msg.success("申请成功");
                    $scope.joinState=1;
                }else{
                    Msg.success("退出成功");
                    $scope.quesList=[];
                }
                
            }
        }, function errorCallback() {
            $scope.Loading = false;
            console.log("连接服务器失败！");
        });
    }
    //收藏专栏
    $scope.collectionCol = function () {
        IS.addFollowSC({columnId:$scope.colInfo.columnId,userId:sysUserInfo.getuserinfo().user_ID }).then(function (response) {
            if (response.errorcode == "0") {
                Msg.success("操作成功");
                if($scope.isCollection){
                    $rootScope.TJInfo.followzlNum = $rootScope.TJInfo.followzlNum - 1; //修改收藏专栏数量
                }else{
                    $rootScope.TJInfo.followzlNum = $rootScope.TJInfo.followzlNum + 1; //修改收藏专栏数量
                }
                $scope.isCollection=!$scope.isCollection;
            } else {
                Msg.error("操作异常!")
            }
        }, function errorCallback() {
            $scope.Loading = false;
            console.log("连接服务器失败！");
        });
    }
    //默认未收藏
    $scope.isCollection=false;
     //是否收藏改专栏
    $scope.getState = function () {
        IS.getIsCollcol({colnumid:$scope.colInfo.columnId,userid:sysUserInfo.getuserinfo().user_ID }).then(function (response) {
            if (response.errorcode == "0") {
                //存在收藏信息
                if(response.datas!=null&&response.datas.length>0){
                    $scope.isCollection=true;
                }
            } else {
                Msg.error("查询收藏状态异常!")
            }
        }, function errorCallback() {
            $scope.Loading = false;
            console.log("连接服务器失败！");
        });
    }
    $scope.getState();

    //置顶或取消置顶
    $scope.ZDQuestion=function(item){
        IS.questionToTop({qaid:item.qaid,userId:sysUserInfo.getuserinfo().user_ID }).then(function (response) {
            if (response.errorcode == "0") {
                item.isTop=!item.isTop;
                if(item.isTop){
                    Msg.success("置顶成功");
                } else{
                    Msg.success("取消成功");
                }  
            } else {
                Msg.error("置顶/取消异常!")
            }
        }, function errorCallback() {
            $scope.Loading = false;
            console.log("连接服务器失败！");
        });
    }
    /*********************************************************************/
    //配置分页
    $scope.paginationConf = {
        currentPage: 1, //$location.search().currentPage ? $location.search().currentPage :  当前页码
        totalItems: 0,  // 总的记录数
        itemsPerPage: 10, // 每页显示多少条        
        perPageOptions: [10, 20],   // 每页显示记录数        
        onChange: function () {
            $scope.paginationConf.currentPage = $scope.paginationConf.currentPage == 0 ? 1 : $scope.paginationConf.currentPage;
            $scope.params.pageIndex = $scope.paginationConf.currentPage;
            $scope.params.pageSize = $scope.paginationConf.itemsPerPage;
            $scope.getList();
        }
    };
    //是否加入专栏
    $scope.isJoinColumn();
} ]);





